import React from 'react';
import { Router, Route, Switch, Redirect } from 'dva/router';
import dynamic from 'dva/dynamic';
import { LocaleProvider } from 'antd';
import zhCn from 'antd/lib/locale-provider/zh_CN';
import App from './pages/Layout';

const RouterConfig = ({ history, app }) => {
  const routes = [
    {
      path: '/login',
      component: () => import('./pages/Login')
    },
    {
      path: '/user/userManage',
      models: () => [import('./models/userManage')],
      component: () => import('./components/User/UserManage')
    },
    {
      path: '/nav/navManage',
      models: () => [import('./models/navManage')],
      component: () => import('./components/Nav/NavManage')
    },
    {
      path: '/article/list',
      models: () => [import('./models/article/list')],
      component: () => import('./components/Article/List')
    },
    {
      path: '/article/create/:id?',
      models: () => [import('./models/article/create')],
      component: () => import('./components/Article/Create')
    },
    {
      path: '/article/category',
      models: () => [import('./models/article/category')],
      component: () => import('./components/Article/Category')
    },
    {
      path: '/pic/list',
      models: () => [import('./models/pic/list')],
      component: () => import('./components/Pic/List')
    },
    {
      path: '/pic/create/:id?',
      models: () => [import('./models/pic/create')],
      component: () => import('./components/Pic/Create')
    }
  ];
  return (
    <Router history={history}>
      <LocaleProvider locale={zhCn}>
        <Switch>
          <App>
            <Switch>
              {
                routes.map(({ path, breadcrumbName, exact = true, ...dynamics }, key) => (
                  <Route
                    key={key}
                    exact={exact}
                    path={path}
                    breadcrumbName={breadcrumbName}
                    component={dynamic({
                      app,
                      ...dynamics
                    })}
                  />
                ))
              }
              <Redirect to='/login' />
            </Switch>
          </App>
        </Switch>
      </LocaleProvider>
    </Router>
  );
};

export default RouterConfig;
